package com.wits.qso.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class GroupCache {
	/**
	 * 
	 * 
	 * 已groupId为分组,成员Id 集合
	 * key: groupId
	 * 
	 * 
	 */
	private static final Map<String,Set<String>> userMap=new HashMap<String,Set<String>>();
	
//    private final ReentrantReadWriteLock rw = new ReentrantReadWriteLock();
//    
//    private final Lock r = rw.readLock();
//    private final Lock w = rw.writeLock();
    
	
	private static GroupCache instance=new GroupCache();
	
	private GroupCache(){
		
	}
	
	public synchronized static GroupCache getInstance() {
		return instance;
	}

	
	/**
	 * 
	 * @param groupId 讨论组Id
	 * @param memberId 用户id
	 */
	public void addMember(String groupId,String memberId){
//		w.lock();
		try{
			if(groupId==null || memberId==null){
				return;
			}
			Set<String> list=userMap.get(groupId);
			if(list==null){
				list=new HashSet<String>();
				userMap.put(groupId,list);
			}
			list.add(memberId);
		}finally{
//			w.unlock();
		}


	}
	
	public void addAll(String groupId,Collection<String> memberIds){
//		w.lock();
		try{
			if(groupId==null || memberIds==null || memberIds.size()==0){
				return;
			}
			Set<String> list=userMap.get(groupId);
			if(list==null){
				list=new HashSet<String>();
				userMap.put(groupId,list);
			}
			list.addAll(memberIds);
		}finally{
//			w.unlock();
		}
	}
	

	/**
	 * 
	 * 用户下线时,将该用户从所有讨论组删除
	 * @param memberId
	 */
	public void removeByMemberId(String memberId) {
//		w.lock();
		try{
			if(memberId==null){
				return;
			}
			for(Set<String> idList:userMap.values()){
				if(idList!=null && idList.contains(memberId)){
					idList.remove(memberId);
				}
			}
		}finally{
//			w.unlock();
		}
	}
	
	/**
	 * 删除讨论组
	 * @param groupId
	 */
	public void remove(String groupId){
//		w.lock();
		try{
			if(groupId==null){
				return;
			}
			userMap.remove(groupId);
		}finally{
//			w.unlock();
		}

	}
	public void removeMember(String groupId,String memberId){
//		w.lock();
		try{
			if(groupId==null || memberId==null){
				return;
			}
			Set<String> list=userMap.get(groupId);
			if(list!=null){
				list.remove(memberId);
			}
		}finally{
//			w.unlock();
		}

		
	}
	
	
	/**
	 * 
	 * @param groupId
	 * @param idList
	 * @return 返回删除之后的
	 */
	public Set<String> removeAll(String groupId,List<String> idList){
//		w.lock();
		Set<String> list=null;
		try{
			if(groupId==null || idList==null){
				return null;
			}
			list=userMap.get(groupId);
			if(list!=null){
				list.removeAll(idList);
			}
		}finally{
//			w.unlock();
		}
		return list;
		
	}
	
	public Set<String> get(String groupId){
//		r.lock();
		try{
			if(groupId==null){
				return null;
			}
			return userMap.get(groupId);
		}finally{
//			r.unlock();
		}

	}
	
	
	
	public Collection<Set<String>> allValues(){
//		r.lock();
		try{
			return userMap.values();
		}finally{
//			r.unlock();
		}
	}

	public Map<String,Set<String>> getUserMap(){
//		r.lock();
		try{
			return userMap;
		}finally{
//			r.unlock();
		}
	}

	public int size(){
//		r.lock();
		try{
			return userMap.size();
		}finally{
//			r.unlock();
		}
	}
	
	public void clear() {
//		w.lock();
		try {
			userMap.clear();
		} finally {
//			w.unlock();
		}
	}


	
}
